const fs = require('fs');
const path = require('path');
const { setImmediate } = require('timers');
var tools = require('../../../libs/tools');
var config = require(tools.rootPath + 'config');
var { connection, pool, model } = require(tools.rootPath + 'libs/model');

var controller = require(tools.rootPath + 'libs/controller.js');


module.exports = class extends controller {
    constructor() {
        super();
    }
    async index(req, res) {
        function doit(i) {
            console.log(i);
        }
        setImmediate(() => {
            //1万次操作6分钟
            for (let i = 1; i <= 10000; i++) {
                setTimeout(() => {
                    doit(i);
                }, 40 * i);

            }
        });



        //下面立即输出
        res.send('ok');
    }

    async index2(req, res) {
        //https://www.html.cn/archives/8810
        function chunk(arr, size) {
            return Array.from({ length: Math.ceil(arr.length / size) }, (v, i) =>
                arr.slice(i * size, i * size + size)
            );
        }

        function get_id_arr(data) {
            let r = [];
            r.push(0);
            for (let d of data) {
                r.push(d.id);
            }
            return r;
        }

        let articles = [
            {
                id: 5,
                uid: 100
            },
            {
                id: 6,
                uid: 100,
            },
            {
                id: 7,
                uid: 100,
            }
        ];

        //console.log(chunk(users, 2));

        let chunks = chunk(articles, 2);
        for (let [index, c] of chunks.entries()) {
            let ids_str = get_id_arr(c).join(',');
            // console.log(ids_str);
            let arcs = await model.select(`select * from article where itemid in(${ids_str})`);
            for (let arc of arcs) {
                console.log(arc.itemid, arc.title);
            }
            console.log("group " + index);
        }

        res.send('ok');
    }

}